Skip to main content

Browser vs NodeJS

JavaScript in the Browser

JavaScript was originally designed to run inside web browsers to make websites interactive. When you open a webpage, the browser downloads the HTML, CSS, and JavaScript, then runs the JavaScript inside the browser engine (like V8 in Chrome, SpiderMonkey in Firefox).

Features in Browser JavaScript

  1. DOM Manipulation
    • Can interact with the Document Object Model (DOM), i.e., change HTML elements, styles, attributes.
    • Example: Changing a button’s text or listening for clicks.
  2. BOM (Browser Object Model)
    • Access to window, navigator, screen, location, etc.
    • Example: Redirecting users to another page with window.location.
  3. Limited File Access
    • For security, browser JS cannot directly read/write local files (except via <input type="file"> or APIs like IndexedDB).
  4. Networking
    • Can make HTTP requests using fetch or XMLHttpRequest.
    • Example: Fetching API data.
  5. Security Sandbox
    • Runs in a restricted environment (sandbox), preventing access to the operating system directly.

JavaScript in Node.js

Node.js is a runtime environment that allows JavaScript to run outside the browser, built on Google’s V8 engine. It is often used for backend development, file systems, servers, and more.

Features in Node.js

  1. No DOM/BOM
    • Node.js does not have document, window, or alert, because it does not run inside a browser.
  2. File System Access
    • Can read, write, and manipulate files with the fs module.
  3. Modules (CommonJS/ESM)
    • Code is organized into reusable modules using require() or import.
  4. Networking & Servers
    • Can create web servers (using built-in http or frameworks like Express).
  5. Access to OS
    • Can use os, path, process modules for system-level operations.
  6. Event-driven architecture
    • Uses event loop, async I/O, and callbacks extensively.

Differences between Browser JS vs Node.js

FeatureBrowser JSNode.js
EnvironmentRuns in browser sandboxRuns in server/OS environment
DOM/BOMAvailable (document, window)Not available
File SystemRestricted (no direct access)Full access via fs module
ModulesES Modules (import/export)CommonJS (require) + ES Modules
Networkingfetch, WebSocketshttp, https, net, Express
Use CaseFrontend (UI, user interactions)Backend (servers, APIs, scripts)
SecurityRuns in sandboxAccess to OS/system resources